Compiler-As-2
Question 1
Given the set of tokens
and answer the following questions. Show the detail of each step.
1. Use the grammar to do left-most and right-most derivation on the input " " and draw the parse tree. (12 pt)
Left-most derivation:
Right-most derivation:
2. Eliminate the left recursions. (6 pt)
There is only one production rule that has left recursion, which is
Eliminate the left recursions:
Due to
can be simplified as follows:
Rewrite the grammar as follows:
3. Base on the grammar without left recursions from Q2, left factorize the grammar. (6 pt)
There are several productions that have common prefixes:
We can left factorize the grammar as follows:
And it can be simplified as follows:
Rewrite the grammar as follows:
4. Base on the grammar from Q3, find the set and the set. (10 pt)
First set
For all terminals:
For
For
For
For
For
For
Final First set:
Follow set:
is the start symbol,
For
For
For
For
For
For
These are terminal symbols. So, Follow(
Final Follow set:
5. Base on the grammar from Q3, construct the parsing table. (6 pt)
6. Base on the original grammar construct the DFA from the set of items (18 pt)
Construct the corresponding augmented grammar
Construct the
For
For
For
For
For
For
For
For
For
For
For
For
For
For
For
For
For
Here is the list of sets of items.
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 |
7. Convert the DFA from Q6 to an parsing table. (15 pt)
0 | |||||||||||||||
1 | |||||||||||||||
2 | |||||||||||||||
3 | |||||||||||||||
4 | |||||||||||||||
5 | 11 | ||||||||||||||
6 | |||||||||||||||
7 | |||||||||||||||
8 | |||||||||||||||
9 | |||||||||||||||
10 | |||||||||||||||
11 | |||||||||||||||
12 | |||||||||||||||
13 | |||||||||||||||
14 | |||||||||||||||
15 | |||||||||||||||
16 | |||||||||||||||
17 | |||||||||||||||
18 | |||||||||||||||
19 | |||||||||||||||
20 |
8. Use the parsing table to parse " ". Show the configuration and the output for each step. (10 pt)
Stack | Input | Output |
---|---|---|
Question 2
Given the following grammar:
9. Show that the grammar is ambiguous. (6 pt)
The grammar
For example the sentence
Obviously, the sentence
10. Find a sentence in the language such that the sentence is ambiguous but left-most derivation and right-most derivation can produce a same parse tree. You need to prove your answer. (9 pt)
For example the sentence
Left-Most Derivation:
- x
Right-Most Derivation: